package com.wkhyapp.lm.fragment;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.MemberAdapter;
import com.wkhyapp.lm.adapter.NoticeAdapter;
import com.wkhyapp.lm.adapter.ShopAdapter;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.IndexPagesPresenter;
import com.wkhyapp.lm.contract.IndexPagesView;
import com.wkhyapp.lm.contract.JXPresenter;
import com.wkhyapp.lm.contract.JXView;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.http.vo.Market;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.http.vo.Shop;
import com.wkhyapp.lm.view.ShopActivity;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//R.layout.fragment_jxpages
public class JXPagesFragment extends SuperFragment<JXPresenter> implements JXView {


    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private ShopAdapter shopAdapter;
    private List<Shop> shops;
    private int mCurrentPage = 1;
    private Integer id;

    public static JXPagesFragment newInstance(Integer createId,String name) {
        JXPagesFragment fragment = new JXPagesFragment();
        Bundle args = new Bundle();
        args.putInt("cid", createId);
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initUI() {
        id = getArguments().getInt("cid");
        shops = new ArrayList<>();
        shopAdapter = new ShopAdapter(R.layout.item_shop,shops);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(shopAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getData(id);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        shopAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        shopAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getShopMore(mCurrentPage,id);

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData(id);
            }
        });


        shopAdapter.setCallBack(new ShopAdapter.callBack() {
            @Override
            public void action(Shop item) {
                goTo(ShopActivity.class,item.getId());
            }
        });

    }


    @Override
    public void setShop(List<Shop> list) {
        shops.clear();
        if(list!=null&&list.size()>0){
            shops.addAll(list);
        }
        shopAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        shopAdapter.loadMoreComplete();

    }

    @Override
    public void setShopMore(List<Shop> list) {
        if(list.size()==0){
            shopAdapter.loadMoreComplete();
            shopAdapter.loadMoreEnd();
        }else{
            shops.addAll(list);
            shopAdapter.notifyDataSetChanged();
            shopAdapter.loadMoreComplete();
        }
    }



    @Override
    public void setMarket(List<Market> list) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_jxpages;
    }

    @Override
    protected JXPresenter createPresenter() {
        return mPresenter = new JXPresenter(this);
    }

}
